package k4_thread.demo02;

import java.util.Random;

public class DownloadFileByRunnable implements Runnable {

    Random random = new Random();
    private String fileName;

    public DownloadFileByRunnable(String fileName) {
        this.fileName = fileName;
    }

    @Override
    public void run() {
        try {
            for (int i = 1; i <=100 ; i++) {
                System.out.println(this.fileName +" 下载了 "+i+"%");
                Thread.sleep(random.nextInt(500)+100);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

class Test1{
    public static void main(String[] args) {

        System.out.println("开始下载了。。。。。。");

        Thread thread1 = new Thread(new DownloadFileByRunnable("java从入门到精通"));
        Thread thread2 = new Thread(new DownloadFileByRunnable("高血压的预防"));
        Thread thread3 = new Thread(new DownloadFileByRunnable("活着"));

        thread1.start();
        thread2.start();
        thread3.start();


        try {
            //先运行当前线程，当前线程运行完毕后再运行主线程
            thread1.join();
            thread2.join();
            thread3.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        System.out.println("下载结束了");

    }
}
